"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DBV认证兼容性
Case Name   : 验证数据库审计功能
Create At   : 2023/3/3
Owner       : @wan005
Description :
    1.配置参数文件
    2.编译并运行java文件
    3.校验数据库审计功能是否正常
Expect      :
    1.修改文件、配置生效
    2.成功编译并运行java文件成功
    3.数据库审计功能正常
History     :
"""
import os
import re
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class JdbcCertify(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.db_primary_user_node = Node(node='PrimaryDbUser')
        self.db_primary_root_node = Node(node='PrimaryRoot')
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.jdbc_path = os.path.join(path, 'dir_certification_0007')
        self.java_name = "jdbc_Certification_Case0007"
        self.common = Common()
        self.constant = Constant()
        self.conf_path = os.path.join(macro.DB_INSTANCE_PATH,
                                 macro.DB_PG_CONFIG_NAME)
        self.conf_path_back = os.path.join(macro.DB_INSTANCE_PATH,
                                 macro.DB_PG_CONFIG_NAME + ".back0007")


    def test_index(self):
        text = '-----1.修改参数audit_enable和audit_dml_state-----'
        self.log.info(text)
        cp_cmd = f'rm -rf {self.conf_path_back} && \
                   /bin/cp {self.conf_path} {self.conf_path_back}'
        self.log.info(cp_cmd)
        msg = self.db_primary_root_node.sh(cp_cmd).result()
        check_cmd = f'ls {self.conf_path_back}'
        msg = self.db_primary_root_node.sh(check_cmd).result()
        self.assertNotIn(self.constant.no_file_msg, msg, "执行失败:" + text)
        add_param1 = f'echo "audit_enabled = on" >> {self.conf_path}'
        self.db_primary_root_node.sh(add_param1)
        add_param2 = f'echo "audit_dml_state = 1" >> {self.conf_path}'
        self.db_primary_root_node.sh(add_param2)
        self.ssh_user = self.db_primary_user_node.ssh_user
        excute_cmd1 = f"ps -u {self.ssh_user}| grep gaussdb | grep -v PID| \
                        awk \'{{{{print $1}}}}\' | xargs kill -1"
        self.log.info(excute_cmd1)
        msg = self.db_primary_root_node.sh(excute_cmd1).result()

        text = '-----2.编译并运行java文件-----'
        self.log.info(text)
        conf_list = {"password":self.db_primary_root_node.db_password,
                     "port":self.db_primary_root_node.db_port,
                     "hostname":self.db_primary_root_node.db_host,
                     "user":self.db_primary_root_node.db_user,
                     "dbname":self.db_primary_root_node.db_name,
                     "stringtype":"unspecified"}
        result_java = self.common.execute_jdbc(conf_list, self.jdbc_path, 
                    self.java_name, self.db_primary_root_node)

        text = '-----3.校验审计功能是否正常-----'
        self.log.info(text)
        self.assertNotIn('ERROR', result_java, "执行失败:" + text)
        self.assertIn('7_01.第0行结果:on', result_java, "执行失败:" + text)
        self.assertIn('7_01.第1行结果:1', result_java, "执行失败:" + text)
        self.assertIn('7_03.结果:insert into', result_java, "执行失败:" + text)


    def tearDown(self):
        text = '-----this is tearDown-----'
        self.log.info(text)
        clean_cmd1 = f'/bin/cp {self.conf_path_back} {self.conf_path}'
        clean_cmd2 = f'rm -rf {self.conf_path_back}'
        self.log.info(clean_cmd1)
        clean1 = self.db_primary_root_node.sh(clean_cmd1).result()
        self.log.info(clean_cmd2)
        clean2 = self.db_primary_root_node.sh(clean_cmd2).result()
        reload_cmd = f"ps -u {self.ssh_user}| grep gaussdb | grep -v PID| \
                       awk \'{{{{print $1}}}}\' | xargs kill -1"
        self.log.info(reload_cmd)
        rld = self.db_primary_root_node.sh(reload_cmd).result()
        check_cmd1 = f'ls {self.conf_path_back}'
        check_cmd2 = f'cat {self.conf_path} | tail -n 2'
        self.log.info(check_cmd1)
        check1 = self.db_primary_root_node.sh(check_cmd1).result()
        self.log.info(check_cmd2)
        check2 = self.db_primary_root_node.sh(check_cmd2).result()
        cmd = f"rm -rf {self.jdbc_path} && ls {self.jdbc_path}"
        self.log.info(cmd)
        msg = self.db_primary_root_node.sh(cmd).result()
        self.assertIn(self.constant.no_file_msg, check1, "执行失败:" + text)
        self.assertNotIn('audit_enabled', check2, "执行失败:" + text)
        self.assertNotIn('audit_dml_state', check2, "执行失败:" + text)
        self.assertIn(self.constant.no_file_msg, msg, "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')